package cn.jy.nvr.ftp2;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.jy.nvr.mapper.DonorInfoMapper;
import cn.jy.nvr.mapper.DonorMapper;
import cn.jy.nvr.mapper.FtpFileNameMapper;
public class SocketThread extends Thread {
	private int port;
	private ServerSocket serverSocket;
	@Autowired
	FtpFileNameMapper fileNameMapper;
	@Autowired
	DonorInfoMapper donorInfoMapper;
	@Autowired
	DonorMapper donorMapper;
	private SocketThread socketThread;
	 
	public void setSocketThread(SocketThread socketThread) {
		this.socketThread = socketThread;
	}
	public SocketThread(ServerSocket serverSocket, int port) {
		this.port=port;
		System.out.println(port);
		if (serverSocket == null) {
			try {
				this.serverSocket = new ServerSocket(port);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void init(){
		System.out.println(socketThread);
		socketThread.start();
		System.out.println("启动socketserver线程");
	}
	
	@Override
	public void run(){
				
		while(!this.isInterrupted()){
			
			try {
				Socket socket=serverSocket.accept();
				if(socket!=null){
					 ProcessSocketData processSocketData=new ProcessSocketData();
					 processSocketData.setSocket(socket,fileNameMapper,donorInfoMapper,donorMapper);
					 processSocketData.start();
					 System.out.println("启动处理线程");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public void closeServerSocket(){
		try {
			if(serverSocket!=null&&!serverSocket.isClosed()){
				serverSocket.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
